/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-1
 * @Time  : 下午3:21
 * @brief :
 */

P.createPage({
    _data: {
        type: 0,
        $refresh:null,
        trade_type: {
            1: '充值',
            2: '提现',
            3: '购买',
            4: '赎回'
        },
        pn: 1,
        status_map: {
            0: 'wait',
            1: 'success',
            2: 'error',
            3: 'wait',
            4: 'error'
        },
        productMap: {
            '1': '活期',
            '2': '一个月定期',
            '23': '三个月定期',
            '26': '六个月定期',
            '3': '锐进宝',
            '4': '果6宝',
            '201': '活期新手通道',
            '999':'系统充值'

        }
    },

    _create: function () {
        var util = require('wap_common:widget/ui/libs/util.js').util,
            $page = $('.page-tradelog'),
            $tab = $page.find('.tab ul'),
            $logContainer = $page.find('.log-container'),
            me = this;

        this.data({
            '$page': $page,
            '$tabContainer': $tab,
            '$tradeTypeTab': $tab.find('li'),
            '$logContainer': $logContainer,
            'util': util
        })
    },

    _init: function () {
        var $tradeTypeTab = this.data('$tradeTypeTab'),
            me = this;

        $tradeTypeTab.bind('touchstart',function () {
            $tradeTypeTab.removeClass('active');
            $(this).addClass('active');
            me.data('type', $(this).find('a').attr('data-tab'));
            me.data('pn',1);
            me.data('$logContainer').empty();
            me.data('$page').find('.ui-refresh-down').hide();
            me.data('$refresh') && me.data('$refresh').refresh('disable','down');
            me._loadData();
        });
        $tradeTypeTab.eq(0).trigger('touchstart');
    },

    createRefresh:function(){
        var me = this,
            $refresh;
        $refresh = $('.body-layout').css('height',window.innerHeight).refresh({
            load: function (dir, type) {
                me._loadData();
            }
        });
        this.data('$refresh',$refresh);
    },

    _loadData: function () {
        var me = this;
        $.getJSON('/user/getusertradelog', {
            'ps': 10,
            'pn': me.data('pn'),
            'type': me.data('type'),
            'date_type': 0
        }, function (ret, status) {
            if (ret.error_no == 0 && ret.data) {
                var list = ret.data.list,
                    $refresh = me.data('$refresh'),
                    pn = me.data('pn'),
                    count = ret.data.count;
                me._createTableList(list);
                me.data('$page').find('.ui-refresh-down').show();
                if($refresh){
                    $refresh.refresh('afterDataLoading','down');
                    if(pn*10<count){
                        $refresh.refresh('enable','down');
                    }else{
                        $refresh.refresh('disable','down');
                    }
                }else if(count>10){
                    me.createRefresh();
                }
                me.data('pn',++pn);
            }
        });
    },
    //购买列表
    _createTableList: function (list) {
        var status = this.data('status'),
            util = this.data('util'),
            tradeType = this.data('trade_type'),
            me = this;
        this.data('$logContainer').append((function () {
            var html = "";
            $.each(list, function (k, v) {
                var pid = null;
                if (!!v.inflow && v.inflow != 100 && v.inflow != 0) {
                    pid = v.inflow;
                } else if (!!v.outflow && v.outflow != 100 && v.outflow != 0) {
                    pid = v.outflow;
                }
                html += '' +
                '<div class="log-block">' +
                '<div class="log-title ' + me.data('status_map')[v.status] + '">' +
                '<span class="icon ' + me.data('status_map')[v.status] + '"></span>' +
                '<span class="log-title-content">' + (pid!=999?tradeType[v.type]:'') + (pid ? me.data('productMap')[pid] : '') + '</span>' +
                '<div class="right">￥' + mathUtil.formatAmount(v.amount) + '</div>' +
                '<i class="triangle triangle-do"></i>' +
                '<span class="time">' + util.toTime(v.time, 'yyyy-MM-dd hh:mm:ss') + '</span>' +
                '<div class="clear"></div>' +
                '</div>' +
                '<ul class="log-content">' +
                '<li>手续费：<span class="right">￥' + mathUtil.formatAmount(v.fee) + '</span></li>' +
                '<li>剩余可用资金：<span class="right">' + ((v.usable_amount == -1) ? '-' : ('￥' + mathUtil.formatAmount(v.usable_amount))) + '</span></li>' +
                '<li>单号：<span class="right">' + v.serial_id + '</span></li>' +
                '</ul>' +
                '</div>';
            });
            return html;
        })());

        $('.log-title').bind('click',function () {
            var me = this;
            $(me).next().toggle();
            $(this).find('.triangle').toggleClass('triangle-up');
            $(this).find('.triangle').toggleClass('triangle-do');
        });
    }

});